package com.sumainfor.wx.tenant.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiniu.util.Json;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.wx.tenant.feign.DevelopmentFeignClient;
import com.sumainfor.wx.tenant.feign.MarketFeignClient;
import com.sumainfor.wx.tenant.service.IndexService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Jack Chen
 * @Create 2019-02-22 15:14
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private DevelopmentFeignClient devFeign;

    @Autowired
    private MarketFeignClient marketFeignClient;

    /**
     * 获得首页Banner 列表数据
     *
     * @return
     */
    @Override
    public JsonResult getIndexBannerList() {
        return devFeign.getIndexBanner();
    }

    /**
     * 获得首页热门房源信息
     *
     * @return
     */
    @Override
    public JsonResult getHotHousingIndexData(Page page) {
        JsonResult<List<Map<String, Object>>> response = devFeign.getHotHousingIndexData();
        if (!response.isSuccess()) {
            return response;
        }
        List<Map<String, Object>> result = response.getResult();
        List<String> houInfoId = result.stream().map(item -> MapUtils.getString(item, "HouInfoId")).collect(Collectors.toList());
        if (houInfoId.size() > 0) {
            return marketFeignClient.getHousingListByIds(CustomMap.create("size", page.getSize())
                    .put("current", page.getCurrent())
                    .put("ids", houInfoId)
            );
        } else {
            return new JsonResult().put(new HashMap<String, Object>(1));
        }

    }

    /**
     * 获得所有的热门房源信息数据
     *
     * @return
     */
    @Override
    public JsonResult getHotHousingAll(Page page) {
        JsonResult resultOne = new JsonResult();
        JsonResult<List<Map<String, Object>>> response = devFeign.getHotHousingAll();
        if (!response.isSuccess()) {
            return response;
        }
        List<Map<String, Object>> result = response.getResult();
        List<String> houInfoId = result.stream().map(item -> MapUtils.getString(item, "HouInfoId")).collect(Collectors.toList());
       if (houInfoId.size()>0) {
           resultOne =  marketFeignClient.getHousingListByIds(CustomMap.create("size", page.getSize())
                   .put("current", page.getCurrent())
                   .put("ids", houInfoId));
       }
       return resultOne;
    }

    /**
     * 获得租房攻略
     *
     * @param page
     * @return
     */
    @Override
    public JsonResult getRaidersMainList(Page page) {
        return devFeign.getRaidersMainList(page);
    }

    /**
     * 根据租房攻略主编号查询所有的内部房源
     *
     * @param page
     * @param raId
     * @return
     */
    @Override
    public JsonResult getRaidersInfoDataByRaId(Page page, String raId) {

        //zc修改租房攻略
        JsonResult resultOne = new JsonResult();
        JsonResult<Map<String, Object>> response = devFeign.getRaidersInfoDataByRaId(page, raId);
        if (!response.isSuccess()) {
            return response;
        }
        List<Map<String, Object>> result = (List<Map<String, Object>>) response.getResult().get("records");
        List<String> houInfoId = result.stream().map(item -> MapUtils.getString(item, "HouInfoId")).collect(Collectors.toList());
        System.out.println();
if (houInfoId.size()>0&&houInfoId!=null) {
    resultOne = marketFeignClient.getHousingListByIds(CustomMap.create("size", page.getSize())
            .put("current", page.getCurrent())
            .put("ids", houInfoId));
}
  return resultOne;
    }

    /**
     * 功能描述:
     * 获取首页活动
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/10 14:34
     */
    public JsonResult getMovableList() {
        return devFeign.getMovableList();
    }

    /**
     * 功能描述:
     * 根据编号获取首页活动详情
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/10 15:25
     */
    public JsonResult getMovableMap(Map<String, Object> params) {
        return devFeign.getMovableMap(params);
    }

    /**
     * 功能描述:
     * 根据活动编号获取活动和房源中间表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/12 18:12
     */
    public JsonResult getMovableHousingList(Page page, Map<String, Object> params) {

        JsonResult resultOne = new JsonResult();
        JsonResult<Map<String, Object>> response = devFeign.getMovableHousingList(params);
        if (!response.isSuccess()) {
            return response;
        }
        List<Map<String, Object>> result = (List<Map<String, Object>>) response.getResult();
        List<String> houInfoId = result.stream().map(item -> MapUtils.getString(item, "HouInfoId")).collect(Collectors.toList());


        if(houInfoId.size()>0&&houInfoId!=null) {
            resultOne =  marketFeignClient.getHousingListByIds(CustomMap.create("size", page.getSize())
                    .put("current", page.getCurrent())
                    .put("ids", houInfoId));
        }
        return resultOne;
    }

}
